package dbdao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lanou.horo.myapplication.dbdao.DaoSession;
import com.lanou.horo.myapplication.dbdao.FristTopDbEntity;
import com.lanou.horo.myapplication.dbdao.HistoryEntity;
import com.lanou.horo.myapplication.dbdao.HistoryEntityDao;
import com.lanou.horo.myapplication.dbdao.RecommendDbEntity;
import com.lanou.horo.myapplication.dbdao.RecommendDbEntityDao;

import java.util.List;

import application.BasisApplication;
import de.greenrobot.dao.query.DeleteQuery;
import de.greenrobot.dao.query.QueryBuilder;

/**
 * Created by dllo on 15/11/30.
 */
public class HistoryDBhelper {
    private static  HistoryDBhelper helper;
    private static DaoSession daoSession;
    private HistoryDBhelper(){

    }
    public static HistoryDBhelper getInstance() {
        if (helper == null) {
            helper = new HistoryDBhelper();
            daoSession = BasisApplication.getDaoSession();//初始化
        }
        return helper;
    }
    /**
     * 提供一个返回cursor对象的方法在provide中调用
     */
    public Cursor queryAllfouse() {
        //获得查询器
        //	QueryBuilder<Themes> aBuilder = daoSession.getThemesDao().queryBuilder();
        SQLiteDatabase database =daoSession.getHistoryEntityDao().getDatabase();//获得数据库对象

        String fouseTabName = daoSession.getHistoryEntityDao().getTablename();//获得表名

        //按照原生的cursor
        Cursor cursor =	database.query(fouseTabName, null, null, null, null, null, null);//获取数据库表中的所有数据

        return cursor;
        // TODO Auto-generated method stub

    }
    /**
     * 将vaules所对应的值插入到数据库中 并返回插入之后的id值
     * @param values
     * @return
     */
    public  long insertProvider(ContentValues values){

        SQLiteDatabase database =daoSession.getHistoryEntityDao().getDatabase();//获得数据库对象

        String fouseTabName = daoSession.getHistoryEntityDao().getTablename();//获得表名


        return  database.insert(fouseTabName, null, values);


    }
    //添加所有
    public void saveHost(List<HistoryEntity> re) {
        // TODO Auto-generated method stub
        daoSession.getHistoryEntityDao().insertInTx(re);//先获得表的DAO
        //daoSession数据库对象 管理都有表 所以用daoSession中get
    }

    //查询所有
    /**
     * 查询数据库中表中的所有数据
     * @return
     */
    public List<HistoryEntity> queryAll() {
        return daoSession.getHistoryEntityDao().loadAll();//loadAll()返回所有数据
    }

    /**
     * 按照id查询
     * @param id
     * @return
     */
    public List<HistoryEntity> querById(long trackId) {
        QueryBuilder<HistoryEntity> db = daoSession.getHistoryEntityDao().
                queryBuilder();//获得表的
        db.where(HistoryEntityDao.Properties.TrackId.eq(trackId));//Properties对应表的属性  id等于传过来的id值

        return db.list();

    }

    public void clearAll(){
        daoSession.getHistoryEntityDao().deleteAll();
    }

    /**
     * 删除
     * @param id
     */
    public void deleById(long id) {
        QueryBuilder<HistoryEntity> qb = daoSession.getHistoryEntityDao().queryBuilder();
        DeleteQuery<HistoryEntity> db = qb.where(HistoryEntityDao.Properties.Id.eq(id)).buildDelete();
        db.executeDeleteWithoutDetachingEntities();

    }


    //更新某个对象
    public void add(HistoryEntity u) {
        daoSession.getHistoryEntityDao().update(u);

    }


    //添加一个对象
    public void addUser(HistoryEntity u) {
//		SQLiteDatabase sq = daoSession.getUSERMSGDao().getDatabase();
//		String tablename = daoSession.getUSERMSGDao().getTablename();
        daoSession.getHistoryEntityDao().insert(u);

    }

    //按id返回某一个对象
    public HistoryEntity getUsermsgByName(long TrackId) {
        QueryBuilder<HistoryEntity> qb = daoSession.getHistoryEntityDao().queryBuilder();
        qb.where(HistoryEntityDao.Properties.TrackId.eq(TrackId));
        List<HistoryEntity> list = qb.list();
        if (qb.list() != null && list.size() > 0) {
    return list.get(0);
}
return null;
        }

//按id更新数据
public HistoryEntity updateByName(long trackId) {
        QueryBuilder<HistoryEntity> qb = daoSession.getHistoryEntityDao().queryBuilder();
        qb.where(HistoryEntityDao.Properties.TrackId.eq(trackId));
        return qb.list().get(0);
        }


    public void update(HistoryEntity user_message) {
        daoSession.getHistoryEntityDao().insertOrReplace(user_message);
    }
}
